﻿using CuteSprite.Eme.Data.Settlement;
using CuteSprite.Eme.UI.Utils;
using HmFramework.Data;
using HmFramework.UI.Utils;
using System;
using System.Windows.Forms;

namespace CuteSprite.Eme.UI.Settlement
{
	public partial class FrmBsdPriceSolutionSingleAdmin : EmeCommonAdminForm
	{
		#region -- 属性 --

		private Int32 _SettlementSolutionID;

		/// <summary>公布价方案ID</summary>
		public Int32 SettlementSolutionID
		{
			get { return _SettlementSolutionID; }
			set { _SettlementSolutionID = value; }
		}

		private Int32 _BillingSolutionID;

		/// <summary>计费方案ID</summary>
		public Int32 BillingSolutionID
		{
			get { return _BillingSolutionID; }
			set { _BillingSolutionID = value; }
		}

		private String _SettlementSolutionCode;

		/// <summary> 公布价方案代码 </summary>
		public String SettlementSolutionCode
		{
			get { return _SettlementSolutionCode; }
			set { _SettlementSolutionCode = value; }
		}

		#endregion 

		#region -- 构造 --

		/// <summary>默认构造函数</summary>

		public FrmBsdPriceSolutionSingleAdmin(Int32 settlementSolutionID, String settlementSolutionCode, Int32 billingSolutionID)
		{
			EntityDataSource = BsdEntityHelper.ModelSettlement;
		//	EntityDataTable = BsdEntityHelper.SSTablePriceSolution;

			SettlementSolutionID = settlementSolutionID;
			BillingSolutionID = billingSolutionID;
			SettlementSolutionCode = settlementSolutionCode;

			InitForm();
		}

		#endregion 

		#region -- 重载自定义查询 --

		/// <summary>重载自定义查询</summary>
		/// <returns></returns>
		protected override WhereExpression MakeCurrentWhereExp()
		{
			if (BillingSolutionID > 0)
			{
				return CurrentEntityFactory.MakeEqual(PriceSolution._.BillingSolutionID, BillingSolutionID);
			}
			else
			{
				return base.MakeCurrentWhereExp();
			}
		}

		#endregion 

		#region -- 新增计费价格方案 --

		/// <summary>新增计费价格方案</summary>
		protected override void OnAddButtonClick()
		{
			// 创建新的实体对象
			IEntity entity = CurrentEntityOperate.Create(true);
			// 排序字段赋值，默认取分页控件的总记录数+1
			entity[EntityHelper.FieldSort] = PageBar.RecordCount + 1;

			var frm = new FrmBsdPriceSolutionSingle(SettlementSolutionID, SettlementSolutionCode, BillingSolutionID, entity);
			var result = frm.ShowDialog(this);
			if (result == DialogResult.OK || result == DialogResult.Yes) { AddEntitySuccess(frm.CurrentEntity); }
			frm.Close();
		}

		#endregion 

		#region -- 编辑计费价格方案 --

		/// <summary>编辑计费方案</summary>
		protected override void OnEditButtonClick()
		{
			if (FindSelectRowCount() < 1)
			{
				DialogHelper.ShowWarning(null, "请选择需要编辑的{0}！".FormatWith(CurrentModelTable.DisplayName));
			}
			else
			{
				var loadEntity = ServerManagement.LoadEntityByKey(CurrentEntityOperate, CurrentModel.Name, CurrentModelTable.Name, SelectedEntity[EntityHelper.FieldPrimaryID]);

				// 创建窗体对象，优先根据编辑模板创建
				var form = new FrmBsdPriceSolutionSingle(SettlementSolutionID, SettlementSolutionCode, loadEntity);
				// 如果没有编辑模板，根据新建模板创建
				var result = form.ShowDialog(this);
				if (result == DialogResult.OK)
				{
					SelectedEntity.CopyFrom(form.CurrentEntity);
					// 编辑成功
					EditEntitySuccess(form.CurrentEntity);
				}
				else if (result == DialogResult.Yes)
				{
					// 另存成功
					AddEntitySuccess(form.CurrentEntity);
				}
			}
		}

		#endregion 

		#region -- 浏览计费价格方案 --

		/// <summary>点击浏览按钮触发</summary>
		protected override void OnViewButtonClick()
		{
			if (FindSelectRowCount() < 1)
			{
				DialogHelper.ShowWarning(null, "请选择需要浏览的{0}！".FormatWith(CurrentModelTable.DisplayName));
			}
			else
			{
				var selEntity = SelectedEntity;

				var loadEntity = ServerManagement.LoadEntityByKey(CurrentEntityOperate, CurrentModel.Name, CurrentModelTable.Name, selEntity[EntityHelper.FieldPrimaryID]);

				// 创建窗体对象，优先根据查看模板创建
				var form = new FrmBsdPriceSolutionSingle(SettlementSolutionID, SettlementSolutionCode, loadEntity, true);
				var result = form.ShowDialog(this);
			}
		}

		#endregion 
	}
}